package com.book.manager.service;

import cn.hutool.core.bean.BeanUtil;
import com.book.manager.dao.RemindMapper;
import com.book.manager.dao.UsersMapper;
import com.book.manager.entity.Users;
import com.book.manager.entity.remind;
import com.book.manager.repos.RemindRepository;
import com.book.manager.repos.UsersRepository;
import com.book.manager.util.ro.PageIn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class RemindService {

    @Autowired
    private RemindRepository remindRepository;

    @Autowired
    private RemindMapper remindMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;


    /**
     * 添加用户
     * @param remind 用户
     * @return 返回添加的用户
     */
    public remind addRemind(remind remind) {
        //向用戶發送郵件

        return remindRepository.saveAndFlush(remind);
    }

    /**
     * 编辑提醒
     * @param remind 用户对象
     * @return true or false
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRemind(remind remind) {
        return remindMapper.updateRemind(BeanUtil.beanToMap(remind))>0;
    }


    /**
     * 提醒详情
     * @param msgid 主键
     * @return 用户详情
     */
    public remind findRemindById(Integer msgid) {
        Optional<remind> optional = remindRepository.findById(msgid);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 删除提醒
     * @param msgid 主键
     * @return true or false
     */
    public void deleteRemind(Integer msgid) {
        remindRepository.deleteById(msgid);
    }


    /**
     * 用户搜索查询(mybatis 分页)
     * @param pageIn
     * @return
     */
    public PageInfo<remind> getRemindList(PageIn pageIn) {

        PageHelper.startPage(pageIn.getCurrPage(),pageIn.getPageSize());
        List<remind> listByLike = remindMapper.findRemindListByLike(pageIn.getKeyword());
        return new PageInfo<>(listByLike);
    }



//    /**
//     * 用户名查询用户信息
//     * @param username 用户名
//     */
//    public Users findByUsername(String username) {
//        return usersRepository.findByUsername(username);
//    }
}
